let mixinList = require('../../common/mixinList');
Page({
    behaviors: [mixinList],
    data: {
        tab_active: 0,
        tabs: [
            { title: '商品', url: '/api/user/collect/index?type=3' },
            { title: '店铺', url: '/api/user/collect/index?type=2' },
        ],
        usePrivateMethod: true, //使用私有方法
        showAddCartPopup: false, //展示加入购物车弹窗
        addCartCurrent: null, //加入购物车商品信息
    },
    onLoad: function (options) {
        if (options.uid) {
            //绑定关系
            return this.redirect_login(options.uid);
        }
        this.getList(true);
    },
    getReqUrl() {
        return this.data.tabs[this.data.tab_active].url;
    },
    getParamsData() {
        return { limit: 20 };
    },
    privateMethod(refresh, res) {
        return new Promise(resolve => {
            if (this.data.tab_active === 0) {
                let result = res.data.filter(item => {
                    //商品已经被供应商删除/下架
                    return !Array.isArray(item.collect_info);
                });
                result = result.map(item => {
                    return item.collect_info;
                });
                resolve(result);
            } else {
                let result = res.data.filter(item => {
                    //供应商被注销
                    return !Array.isArray(item.collect_info);
                });
                result.forEach(item => {
                    if (item.collect_info.face)
                        item.collect_info.face =
                            item.collect_info.face + '!img_100';
                    if (item.collect_info.logo)
                        item.collect_info.logo =
                            item.collect_info.logo + '!img_100';
                });
                resolve(result);
            }
        });
    },
    /**tabs切换事件 */
    onChange(event) {
        this.setData({
            tab_active: event.detail.index,
            list: [],
            empty: true,
        });
        this.getList(true);
    },
    operation({ mark }) {
        const { index, addCart } = mark;
        if (index == undefined) return;
        if (addCart) {
            return this.openAddcartPopup(index);
        }
        this.toDetail(index);
    },
    //前往商品详情
    toDetail(index) {
        wx.navigateTo({
            url: `/pages/good_detail/index?id=${this.data.list[index].product_id}`,
        });
    },
    /**点击加入购物车 */
    openAddcartPopup(index) {
        const info = this.data.list[index];

        if (info.sku === 0)
            return wx.showToast({
                title: '该商品已售空',
                icon: 'none',
            });
        const { imgHost } = this.data;
        const addCartCurrent = {
            product_img: imgHost + info.product_img,
            product_name: info.product_name,
            product_price: info.product_price,
            sku: info.sku,
            unit: info.unit,
            supplier_id: info.supplier.supplier_id,
            supplier_name: info.supplier.title,
            level: info.level,
            norm: info.norm,
            product_id: info.product_id,
            sell_num: info.sell_num,
        };

        this.setData({
            addCartCurrent,
            showAddCartPopup: true,
        });
    },
});
